<template>
  <div class="gallery-list" @mouseenter="isHover = true" @mouseleave="isHover = false">
    <div class="gallery-container gallery-caption-round container-content" :style="{backgroundColor: modelValue?.bgColor}">
      <div class="default-row row" :class="[`row-cols-${modelValue?.mobileCol}`, `row-cols-md-${modelValue?.desktopCol}`,  modelValue?.center && 'row-center']">
        <div v-for="item in modelValue?.data" :key="item.id" class="col">
          <div class="gallery-caption-round gallery-caption" :class="[item.url && 'has-url']">
            <!-- 图片部分 -->
            <!-- 如果链接包含 http 则使用 a 标签跳转 -->
            <a v-if="item.url && item.url.includes('http')" :href="item.url" target="_blank" class="gallery-caption__img">
              <img :src="item.imgSrc" :alt="item.alt">
            </a>
            <!-- 否则使用 button 并 emit handleClick 事件 -->
            <button v-else-if="item.url" @click="handleClick(item.url)" class="gallery-caption__img">
              <img :src="item.imgSrc" :alt="item.alt">
            </button>
            <div v-else class="gallery-caption__img">
                <img :src="item.imgSrc" :alt="item.alt" />
            </div>

            <!-- 标题部分 -->
            <!-- 如果链接包含 http 则使用 a 标签跳转 -->
            <a v-if="item.url && item.url.includes('http')" :href="item.url" target="_blank" class="gallery-caption__title tx-subtitle text-blue text-center">
              {{ item.title }}
            </a>
            <!-- 否则使用 button 并 emit handleClick 事件 -->
            <button v-else-if="item.url" @click="handleClick(item.url)" class="gallery-caption__title tx-subtitle text-blue text-center">
              {{ item.title }}
            </button>
            <div v-else class="tx-subtitle text-blue text-center" v-html="item.title"></div>
          </div>
        </div>
      </div>
    </div>

     <Operate v-if="!isPreview" v-model:show="isHover" @handle-edit="showSetting = true" @handle-delete="handleDelete" @handle-copy="handleCopy" @handle-paste="handlePaste"></Operate>
  </div>

  <Setting v-if="!isPreview" v-model:show="showSetting" :data="modelValue"></Setting>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { randomString } from '@/utils/common'
import Setting from './setting.vue'
import Operate from '@/common/Operate/index.vue'

// 定义组件名称
defineOptions({
  name: 'GalleryCaptionRound'
})

const props = defineProps({
  modelValue: {
    type: Object,
    default: () => {}
  },

  // 是否预览
  isPreview: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['update:modelValue', 'handleDelete', 'handleCopy', 'handlePaste', 'handleClick'])

const handleInit = () => {
  if (props.modelValue && props.modelValue.data) return // 有数据不用初始化
  const data = {
    id: props.modelValue?.id || randomString(),
    type: 'GalleryCaptionRound',
    bgColor: '',
    desktopCol: 3,
    mobileCol: 2,
    center: false,
    data: [
      {
        id: randomString(),
        title: '标题',
        imgSrc: 'https://osswebsite-test.cedim.cn/media-library/ywies-bj/images/home/ywies-tx.jpg',
        alt: '',
        url: 'http://baidu.com'
      },
      {
        id: randomString(),
        title: '标题',
        imgSrc: 'https://osswebsite-test.cedim.cn/media-library/ywies-bj/images/home/ywies-tx.jpg',
        alt: '',
        url: ''
      },
    ]
  }
  emit('update:modelValue', data)
} 

const isHover = ref<boolean>(false);
const showSetting = ref<boolean>(false);

const handleDelete = () => {
  emit('handleDelete', props.modelValue.id)
}

const handleCopy = () => {
  emit('handleCopy', props.modelValue)
}

const handlePaste = (type: string) => {
  emit('handlePaste', type)
}

const handleClick = (url: string) => {
  emit('handleClick', url)
}

handleInit()
</script>

<style lang="scss" scoped>
/* 让 button 看起来像链接 */
.gallery-caption button {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
  font: inherit;
  width: 100%;
  text-align: left;
}

.gallery-list {
  position: relative;
  width: 100%;
}

.gallery-list .row-center {
  justify-content: center;
}

.gallery-caption {
  width: 100%;
  height: 100%;
}

/* Simple row/col grid with responsive row-cols helpers used by this component */
.row {
  --bs-gutter-x: 16px;
  --bs-gutter-y: 0;
  display: flex;
  flex-wrap: wrap;
  margin-top: calc(var(--bs-gutter-y)*-1);
  margin-right: calc(var(--bs-gutter-x)*-0.5);
  margin-left: calc(var(--bs-gutter-x)*-0.5)
}

.row>* {
  flex-shrink: 0;
  width: 100%;
  max-width: 100%;
  padding-right: calc(var(--bs-gutter-x)*0.5);
  padding-left: calc(var(--bs-gutter-x)*0.5);
  margin-top: var(--bs-gutter-y)
}

.col {
  flex: 1 0 0%
}

.row-cols-auto>* {
  flex: 0 0 auto;
  width: auto
}

.row-cols-1>* {
  flex: 0 0 auto;
  width: 100%
}

.row-cols-2>* {
  flex: 0 0 auto;
  width: 50%
}

.row-cols-3>* {
  flex: 0 0 auto;
  width: 33.3333333333%
}

.row-cols-4>* {
  flex: 0 0 auto;
  width: 25%
}

.row-cols-5>* {
  flex: 0 0 auto;
  width: 20%
}

.row-cols-6>* {
  flex: 0 0 auto;
  width: 16.6666666667%
}

@media(min-width: 768px) {
  .row-cols-md-auto>* {
      flex: 0 0 auto;
      width: auto
  }

  .row-cols-md-1>* {
      flex: 0 0 auto;
      width: 100%
  }

  .row-cols-md-2>* {
      flex: 0 0 auto;
      width: 50%
  }

  .row-cols-md-3>* {
      flex: 0 0 auto;
      width: 33.3333333333%
  }

  .row-cols-md-4>* {
      flex: 0 0 auto;
      width: 25%
  }

  .row-cols-md-5>* {
      flex: 0 0 auto;
      width: 20%
  }

  .row-cols-md-6>* {
      flex: 0 0 auto;
      width: 16.6666666667%
  }
}

/* image wrapper helpers */
.img-wrapper {
  position: relative;
  height: 0;
  width: 100%
}
.img-wrapper img {
  position: absolute;
  width: 100%;
  height: 100%;
  -o-object-fit: cover;
  object-fit: cover
}

.tx-subtitle {
  font-size: 16px;
  font-weight: 700;
  line-height: 1.25;
  letter-spacing: .1em
}

@media(max-width: 1023.98px) {
  .tx-subtitle {
      font-size:14px;
      line-height: 1.714
  }
}

@media(max-width: 767.98px) {
  .tx-subtitle {
      line-height:1.428
  }
}

.text-blue {
  --bs-text-opacity: 1;
  color: rgba(0,72,232,var(--bs-text-opacity))!important;
}

.text-center {
  text-align: center!important
}

@media(min-width: 1280px) {
  .gallery-list .gallery-container.gallery-caption-round {
    --max-width:clamp(952px,calc(76.27765vw - 24.35393px),1440px);
  }
}

.gallery-list .gallery-container.gallery-caption-round .caption-row {
  --bs-gutter-x: 18px;
  --bs-gutter-y: 24px;
}

@media(max-width: 767.98px) {
  .gallery-list .gallery-container.gallery-caption-round .caption-row {
    --bs-gutter-x:10px;
    --bs-gutter-y: 13.14px;
  }
}

.gallery-caption__img {
  border-radius: 8px;
}

.gallery-caption__img img {
  width: 100%;
  height: auto;
  -o-object-fit: contain;
  object-fit: contain;
  transition: transform .3s ease-in-out;
  transform: scale(1.05);
}

.gallery-caption__title {
  text-decoration: none;
  display: block;
  margin-top: 8.4px;
}

@media(max-width: 767.98px) {
  .gallery-caption__title {
    margin-top:5px;
  }
}

.gallery-caption__title,.gallery-caption__title * {
  font-size: 16px;
  letter-spacing: .01em;
  line-height: 1.5;
  color: #0032a0;
  color: var(--text-color--primary,#0032a0);
  font-weight: 500;
}

@media(max-width: 1023.98px) {
  .gallery-caption__title,.gallery-caption__title * {
    font-size:12px;
  }
}

.gallery-caption.has-url .gallery-caption__img {
  display: block;
  overflow: hidden;
}

.gallery-caption.has-url .gallery-caption__img img:hover {
  transform: scale(1);
}
</style>